﻿using System.Collections;
using System.Windows.Controls;
using System.Windows.Media;
using Algoritmos_de_Ordenação.Algoritmos;

namespace Algoritmos_de_Ordenação
{

    public enum TabType
    {
        Bubble,
        Heap,
        Insertion,
        Quick,
        QuickRandom,
        Selection,
        Shell,
        All
    }

    public class Controls
    {
        ArrayList activeTextBoxes = new ArrayList();
        DrawingRegion dr = new DrawingRegion();
        SpeedClicked velocity = SpeedClicked.Normal;

        ArrayList activeSortAlgs = new ArrayList();

        public void addPermanentControls (Canvas cv)
        {
           Images myImages = new Images();
           myImages.addImages(cv);
        }
        
        internal void addComponents(TabItem selectedTab, Canvas cvCompareAll)
        {
            
            activeSortAlgs.Clear();

            activeTextBoxes.Clear();

            #region Create algorithms
            BubbleSort bs = new BubbleSort(selectedTab);
            activeSortAlgs.Add(bs);

            HeapSort hs = new HeapSort(selectedTab);
            activeSortAlgs.Add(hs);

            InsertionSort ins = new InsertionSort(selectedTab);
            activeSortAlgs.Add(ins);

            QuickSort qs = new QuickSort(selectedTab);
            activeSortAlgs.Add(qs);

            QuickSortRandom qsr = new QuickSortRandom(selectedTab);
            activeSortAlgs.Add(qsr);

            SelectionSort ss = new SelectionSort(selectedTab);
            activeSortAlgs.Add(ss);

            ShellSort shs = new ShellSort(selectedTab);
            activeSortAlgs.Add(shs);
            #endregion

            foreach (DrawingRegion dreg in activeSortAlgs)
            {
                dreg.newWidth = 128;
                dreg.isCompareAll = true;
                dreg.updateNumberArraySize(9);
                Canvas.SetLeft(dreg, activeSortAlgs.IndexOf(dreg) * 137 + 6);
                createTextBox(cvCompareAll, activeSortAlgs.IndexOf(dreg));
                cvCompareAll.Children.Add(dreg);
            }
            selectedTab.Content = cvCompareAll;
        }

        private void createTextBox(Canvas cvCompareAll, int i)
        {
            TextBox tb = new TextBox();

            tb.Width = 125;
            tb.Height = 70;
            tb.Background = Brushes.Transparent;
            tb.Foreground = Brushes.LawnGreen;
            tb.FontSize = 10.5;
            tb.FontFamily = new FontFamily("Helvetica");

            Canvas.SetLeft(tb, i * 137 + 7);
            Canvas.SetTop(tb, 360);

            activeTextBoxes.Add(tb);
            cvCompareAll.Children.Add(tb);
        }
        
        internal void addComponents(TabType tabType, TabItem selectedTab, Canvas cvMain)
        {
            switch(tabType)
            {
                case TabType.Bubble: dr = new BubbleSort(selectedTab);
                    break;
                case TabType.Heap: dr = new HeapSort(selectedTab);
                    break;
                case TabType.Insertion: dr = new InsertionSort(selectedTab);
                    break;
                case TabType.Quick: dr = new QuickSort(selectedTab);
                    break;
                case TabType.QuickRandom: dr = new QuickSortRandom(selectedTab);
                    break;
                case TabType.Selection: dr = new SelectionSort(selectedTab);
                    break;
                case TabType.Shell: dr = new ShellSort(selectedTab);
                    break;
            }
            
            activeSortAlgs.Clear();
            activeSortAlgs.Add(dr);

            dr.isCompareAll = false;
            dr.updateGBThread(-1);
            
            cvMain.Children.Add(dr);

            selectedTab.Content = cvMain;
        }

        public void buttonClicked(ButtonClicked bc)
        {
            switch (bc)
            {
                case ButtonClicked.Increasing: Buttons.btnIncreasing_Click(activeSortAlgs);
                    break;
                case ButtonClicked.Decreasing: Buttons.btnDecreasing_Click(activeSortAlgs);
                    break;
                case ButtonClicked.Random: Buttons.btnRandomica_Click(activeSortAlgs);
                    break;                
                case ButtonClicked.Pause: Buttons.btnPausa_Click(activeSortAlgs);
                    break;
                case ButtonClicked.Reset: Buttons.btnReset_Click(activeSortAlgs);
                    break;
                case ButtonClicked.Resume: Buttons.btnResume_Click(activeSortAlgs);
                    break;
            }
        }

        public void buttonClicked(ButtonClicked bc, Button[] btnsToUpdate)
        {
            switch (bc)
            {
                case ButtonClicked.Sort: Buttons.btnSort_Click(activeSortAlgs, btnsToUpdate, velocity);
                    break;
                case ButtonClicked.Stop: Buttons.btnStop_Click(activeSortAlgs, btnsToUpdate);
                    break;
            }
        }

        public void removeDrawingRegions(Canvas cv)
        {
            foreach (DrawingRegion dreg in activeSortAlgs)
            {
                if (cv.Children.Contains(dreg))
                {
                    cv.Children.Remove(dreg);
                }
            }
        }

        public void removeTextBoxes(Canvas cv)
        {
            foreach (TextBox tb in activeTextBoxes)
            {
                if (cv.Children.Contains(tb))
                {
                    cv.Children.Remove(tb);
                }
            }
        }
                
        public void removeComponentes(TabItem tab)
        {
            tab.ClearValue(TabItem.ContentProperty);
        }

        public void menuClicked(SpeedClicked velocityClicked, MenuItem[] MenuItems)
        {
            velocity = velocityClicked;

            foreach (DrawingRegion dreg in activeSortAlgs)
            {
                dreg.SortSpeed = (int) velocity;
            }
            
            Menus.updatedItemChecked(velocity, MenuItems);
        }

        public void stopThreads()
        {
            foreach (DrawingRegion dreg in activeSortAlgs)
            {
                dreg.Stop();
            }
        }

        internal void setShowTrace(bool showTrace)
        {
            foreach (DrawingRegion dreg in activeSortAlgs)
            {
                dreg.showTrace = showTrace;
            }
        }
    }
}
